package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService attrValueService;
    @Autowired
    private RoomFacilityService facilityInfoService;
    @Autowired
    private RoomLabelService labelInfoService;
    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //如果更新先删除room信息
        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);


        //
        if (isUpdate) {
            //删除图片
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(lambdaQueryWrapper);


            //删除属性
            LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            attrValueService.remove(lambdaQueryWrapper1);

            //删除配套
            LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            facilityInfoService.remove(lambdaQueryWrapper2);

            //删除标签
            LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            labelInfoService.remove(lambdaQueryWrapper3);

            //删除支付
            LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper4.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            paymentTypeService.remove(lambdaQueryWrapper4);

            //删除租期
            LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper5.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            leaseTermService.remove(lambdaQueryWrapper5);

        }

        //保存图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());

                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }


        //属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> objects = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                //加注解不能new
                RoomAttrValue build = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                objects.add(build);
            }
            attrValueService.saveBatch(objects);
        }

        //配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<RoomFacility> objects = new ArrayList<>();

            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                objects.add(build);
            }

            facilityInfoService.saveBatch(objects);
        }

        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> objects = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel build = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                objects.add(build);
            }
            labelInfoService.saveBatch(objects);
        }
        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> objects = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();

                objects.add(build);

            }
            paymentTypeService.saveBatch(objects);
        }


        //租期

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> objects = new ArrayList<>();

            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();

                objects.add(build);

            }

            leaseTermService.saveBatch(objects);
        }


    }

    @Override
    public Page<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //查询RoomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        //属性
        List<AttrValueVo>attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //配套
        List<FacilityInfo>facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        //标签
        List<LabelInfo> labelInfoList= labelInfoMapper.selectListByRoomId(id);
        //支付方式
        List<PaymentType>paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        //租期
        List<LeaseTerm>leaseTermList = leaseTermMapper.selectListByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;


    }

    @Override
    public void removeRoomById(Long id) {
        //删除roominfo
        super.removeById(id);
        //删除图片    new条件选择器 ---  类型,id(直接id会都删掉的)
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        lambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(lambdaQueryWrapper);
        //删除属性
        LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId,id);
        attrValueService.remove(lambdaQueryWrapper1);
        //删除配套
        LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(RoomFacility::getRoomId,id);
        facilityInfoService.remove(lambdaQueryWrapper2);
        //删除标签
        LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(RoomLabel::getRoomId,id);
        labelInfoService.remove(lambdaQueryWrapper3);
        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper4.eq(RoomPaymentType::getRoomId,id);
        paymentTypeService.remove(lambdaQueryWrapper4);
        //删除租期
        LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(RoomLeaseTerm::getRoomId,id);
        leaseTermService.remove(lambdaQueryWrapper5);
    }
}




